package com.example.parking.controller;

import com.example.parking.entity.ParkingSpace;
import com.example.parking.entity.Vehicle;
import com.example.parking.service.ParkingSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/parking-spaces")
public class ParkingSpaceController {
    @Autowired
    private ParkingSpaceService parkingSpaceService;

    // 获取所有车位信息
    @GetMapping
    public List<ParkingSpace> getAllParkingSpaces() {
        return parkingSpaceService.getAllParkingSpaces();
    }

    // 条件查询车位
    @GetMapping("/search")
    public List<ParkingSpace> searchParkingSpaces(
            @RequestParam(required = false) String spaceNumber,
            @RequestParam(required = false) String area) {
        return parkingSpaceService.searchParkingSpaces(spaceNumber, area);
    }

    // 统计空闲车位数量
    @GetMapping("/available-count")
    public int countAvailableSpaces() {
        return parkingSpaceService.countAvailableSpaces();
    }

    // 分配车位
    @PostMapping("/{spaceId}/assign")
    public ParkingSpace assignParkingSpace(
            @PathVariable Long spaceId,
            @RequestBody Vehicle vehicle) {
        return parkingSpaceService.assignParkingSpace(spaceId, vehicle);
    }

    // 回收车位
    @PostMapping("/{spaceId}/release")
    public ParkingSpace releaseParkingSpace(@PathVariable Long spaceId) {
        return parkingSpaceService.releaseParkingSpace(spaceId);
    }
}